package com.lenovo.vod.player.setting.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lenovo.vod.player.setting.util.MediaDisplayArrayAdapter;
import com.lenovo.vod.player.setting.util.StringUtils;
import com.lenovo.vod.player.R;

/**
 *Adapter
 * 
 * @author lenovo
 * 
 */
public class MediaDisplayArrayAdapter extends BaseAdapter {

    private List<String> mObjects;
    private Context mContext;
    private LayoutInflater mInflater;
    private String mObjectName;
    private List<String> mDifferentDatas = new ArrayList<String>();
    private List<String> mTransferArgument = new ArrayList<String>();
    private String mSelfAda;
    private List<String> mDefintions;

    public MediaDisplayArrayAdapter(Context context, String[] objects,
            String textArrayValue, List<String> differents,
            List<String> transferArgument, String selfAda,List<String> defintions) {
        init(context, Arrays.asList(objects), textArrayValue, differents,
                transferArgument, selfAda ,defintions);
    }

    public MediaDisplayArrayAdapter(Context context, String[] objects) {
        this.mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mObjects = Arrays.asList(objects);
    }

    public MediaDisplayArrayAdapter(Context context) {
        this.mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * set different datas
     * @param dDatas
     * @param textArrayValue
     * @param transferArgument
     */
    public void differentDatas(List<String> dDatas, String textArrayValue,
            List<String> transferArgument , String [] definitions) {
        this.mDifferentDatas = dDatas;
        this.mObjectName = textArrayValue;
        this.mTransferArgument = transferArgument;
        this.mDefintions = StringUtils.getStringArrayByResourceId(definitions);
        this.notifyDataSetChanged();
    }

    private void init(Context context, List<String> objects,
            String textArrayValue, List<String> differents,
            List<String> transferArgument, String selfAda , List<String> defintions) {
        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mObjects = objects;
        mObjectName = textArrayValue;
        mDifferentDatas = differents;
        mTransferArgument = transferArgument;
        mSelfAda = selfAda;
        mDefintions = defintions;
        
    }

    // override
    public int getCount() {
        return mObjects.size();
    }

    // override
    public String getItem(int position) {
        return mObjects.get(position);
    }

    // override
    public int getPosition(String item) {
        return mObjects.indexOf(item);
    }

    // not override
    public long getItemId(int position) {
        return position;
    }

    // override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = mInflater.inflate(
                    R.layout.vodplayer_setting_option_listitems, parent, false);
        } else {
            view = convertView;
        }

        TextView text = (TextView) view
        .findViewById(R.id.vodplayer_setting_tvOption);
        

        String item = getItem(position);
        /*
         * text.setTextColor(Color.WHITE);
        String currentItem = mDefintions.get(position);

        if (mSelfAda.equals(currentItem) && mSelfAda.equals(mObjectName)) {
            text.setTextColor(Color.BLACK);
        } else if (mSelfAda.equals(currentItem) && !mSelfAda.equals(mObjectName)) {
            text.setTextColor(Color.WHITE);
        } else {
            if (mTransferArgument == null || mTransferArgument.size() == 0
                    || mTransferArgument.isEmpty()) {
                text.setTextColor(Color.GRAY);
            } else {
                if (mDifferentDatas != null && mDifferentDatas.size() > 0
                        && !mDifferentDatas.isEmpty()) {
                    if (mDifferentDatas.contains(currentItem)) {
                        text.setTextColor(Color.GRAY);
                    } else {
                        if (mObjectName.equals(currentItem)) {
                            text.setTextColor(Color.BLACK);
                        } else {
                            text.setTextColor(Color.WHITE);
                        }
                    }
                } else {
                    if (mObjectName != null && !"".equals(mObjectName)) {
                        if (mObjectName.equals(currentItem)) {
                            text.setTextColor(Color.BLACK);
                        }
                    }
                }
            }
        }
        
        if (item instanceof CharSequence) {
            text.setText((CharSequence) item);
        } else {
            text.setText(item.toString());
        }
	*/
        return view;

    }

    public static MediaDisplayArrayAdapter createFromResource(Context context,
            int textArrayResId) {
        String[] strings = context.getResources()
                .getStringArray(textArrayResId);
        return new MediaDisplayArrayAdapter(context, strings);
    }

    public static MediaDisplayArrayAdapter createFromResource(Context context,
            int textArrayResId, String textArrayValue, List<String> differents,
            List<String> transferArgument, String selfAda , String [] mDefins) {
        String[] strings = context.getResources()
                .getStringArray(textArrayResId);
        List<String> mDefintions = StringUtils.getStringArrayByResourceId(mDefins);
        return new MediaDisplayArrayAdapter(context, strings, textArrayValue,
                differents, transferArgument, selfAda , mDefintions);
    }
}
